import { View } from '@tarojs/components';
import Taro, { Events } from '@tarojs/taro'
import { useState, useEffect, useMemo, useLayoutEffect } from 'react'
import Header from '../../components/navCustomBar'
import NewsInfo from '../newsInfo';
import { getStorage, getBrowserController } from '../../components/utils';
import { connect, useDispatch } from 'react-redux'
import Utils from '../../../utils/index'
import './index.scss'



const storage = getStorage()
const browserController = getBrowserController()
const deviceType = browserController.deviceType()

let ClientType
switch (deviceType) {
    case 'web':
        ClientType = 1
        break;
    case 'ios':
        ClientType = 3
        break;
    case 'android':
        ClientType = 2
        break;

    default:
        ClientType = 42;
}

let timer: any = null

function App(props) {

    const {
        userInfo,
        isManage
    } = props

    const dispatch = useDispatch()
    const [wsClient, setwsClient] = useState<any>()
    const getToken = () => {
        if (Utils.getQueryVariable('env') === 'weapp') {
            return Utils.getQueryVariable('lg_tk')
        } else {
            return storage.get('token') || Utils.getQueryVariable('lg_tk')
        }
    }

    useEffect(() => {
        if (isManage === 1) {
            // document.title = "周行事历管理"
        } else {
            // document.title = "周行事历"
        }
        // getReadInfo()
    }, [])

    useEffect(() => {
        return () => {
            clearInterval(timer)
            timer = null
        }
    }, [wsClient])


    return (
        <View className='newsInfo_app'>
            <View className='newsInfo_top'>
                {
                    Taro.getInWebview() ? "" :
                        <Header
                            isTitlePositionLeft={false}
                            mainTitle={isManage === 1 ? '周行事历管理' : '周行事历'}
                            mainTitleBold={true}
                            needBackIcon={process.env.TARO_ENV === 'weapp' ? false : true}
                            needMainIcon={process.env.TARO_ENV === 'weapp' ? false : true}
                        />
                }

            </View>
            <NewsInfo />
        </View>
    )
}

const mainProps = ({ commonData }) => {
    return {
        userInfo: commonData.userInfo,
        isManage: commonData.isManage,
    }
}

export default connect(mainProps)(App)
